package com.example.a1.zyfypt325cht.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a1.zyfypt325cht.R;
import com.example.a1.zyfypt325cht.activity.LoginActivity;
import com.example.a1.zyfypt325cht.iface.LogoutListener;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Neusoft on 2017/4/9.
 */


public class Fragment6 extends Fragment implements View.OnClickListener {
    private TextView tvinfo,ivpass;
    private Button btn_exit;
    private View view = null;
    private Context context;
    private SharedPreferences sp;
    private String sessionid = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        //动态加载Fragment6的布局文件
        view = inflater.inflate(R.layout.fragment6, container, false);
        //获取布局上的控件，注意通过view获取
        init();
//       tvinfo.setText("我");
        //注意Toast第一个参数上下文
        Toast.makeText(context, "加载我", Toast.LENGTH_SHORT).show();
        //返回动态生成的view
        return view;
    }

    private void init() {
        tvinfo = (TextView) view.findViewById(R.id.textViewMyPage);
        ivpass = (TextView) view.findViewById(R.id.textViewMyPage2);
        btn_exit = (Button) view.findViewById(R.id.button_exit);
        btn_exit.setOnClickListener(this);
        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        sessionid = sp.getString("sessionID", null);
        tvinfo.setText(sp.getString("username", null));
        ivpass.setText(sp.getString("password",null));
    }

    LogoutListener logoutListener = new LogoutListener() {
        @Override
        public void onResponse(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, LoginActivity.class);
            startActivity(intent);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_exit:
                //退出登录
//                LogoutModel logoutModel = new LogoutModel();
//                logoutModel.getResult(sessionid, logoutListener);
                Toast.makeText(context, "注销登录成功", Toast.LENGTH_SHORT).show();
                getActivity().onBackPressed();//模仿推出登陸
                break;
            default:
                break;
        }
    }
}

